<?php
declare (strict_types=1);

namespace app\admin\controller;


use app\common\builder\TableBuilder;
use app\common\builder\TableFront;
use think\App;
use think\facade\View;


class Config extends Base
{
    //主键，默认为id
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\Config';

    //验证器
    protected $validate = 'Config';

    public function index()
    {
        return $this->setTable();
    }

    public function setTable()
    {
        //构建table
        $table = [
            ['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80],
            ['field' => 'name', 'title' => '名称'],
            ['field' => 'title', 'title' => '标题'],
            ['field' => 'type', 'title' => '类型'],
            ['field' => 'group', 'title' => '分组'],
            ['field' => 'create_time', 'title' => '创建时间'],
            ['field' => 'update_time', 'title' => '修改时间'],
            ['field' => 'status', 'title' => '状态', 'templet' => '#status', 'width' => 120, 'unresize' => true],
            ['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作']
        ];
        return TableBuilder::getInstance()
            ->addColumns(TableFront::getInstance()->addBatchRows($table))
            ->addTopButtons(['add' => ['width' => 1000, 'height' => 1000]])
            ->addRightButtons(['edit' => ['width' => 1000, 'height' => 1000], 'delete'])
            ->addSwitchTool([['name' => 'status', 'pk' => 'id', 'text' => '开启|关闭']])
            ->setSearch($this->setSearch())
            ->fetch();
    }

    public function setSearch()
    {
        return TableFront::getInstance()
            ->setSearch(['name__title', '名称/标题', 'input', '', '', '', 'like']);
    }


    public function create($id = 1)
    {

        $map = array('status' => 1, 'group' => $id, 'is_del' => 0);
        View::assign('list', $this->model::where($map)->order('sort', 'asc')->select());
        View::assign('id', $id);
        return View::fetch();
    }

    public function saveGroup()
    {
        return json($this->model::saveGroup($this->request->param('config'), $this->admin));
    }

    public function add($name = '', $tpl = 'config/add')
    {
        return parent::add($name, $tpl); // TODO: Change the autogenerated stub
    }


    public function edit($name = '', $id = '', $tpl = 'config/edit', $info = '')
    {
        return parent::edit($name, $id, $tpl, $info); // TODO: Change the autogenerated stub
    }


}
